﻿using System;
using System.Collections.Generic;
using System.Text;
using SAServices.DAL;
using System.IO;

namespace SAServices.BLL
{
    public class FamilyBLL
    {
        internal static bool AddFamilyInfo(Model.ConnectionConext connCtx, Model.FamilyInfo familyInfo)
        {
            return FamilyDAL.AddFamilyInfo(connCtx, familyInfo);
        }

        internal static Model.FamilyInfo GetFamilyInfo(Model.ConnectionConext connCtx, Guid familyId)
        {
            return FamilyDAL.GetFamilyInfo(connCtx, familyId);
        }

        internal static List<Model.FamilyInfo> GetFamilyInfoNew(Model.ConnectionConext connCtx)
        {
            return FamilyDAL.GetFamilyInfoNew(connCtx);
        }
        internal static bool UpdateFamilyInfo(Model.ConnectionConext connCtx, Model.FamilyInfo familyInfo)
        {
            return FamilyDAL.UpdateFamilyInfo(connCtx, familyInfo);
        }

        internal static bool DeleteFamilyInfo(Model.ConnectionConext connCtx, Guid familyId)
        {
            return FamilyDAL.DeleteFamilyInfo(connCtx, familyId);
        }

        internal static List<Model.FamilyInfo> GetFamiliesByCategoryId(Model.ConnectionConext connCtx, Guid cateId)
        {
            return FamilyDAL.GetFamiliesByCategoryId(connCtx, cateId);
        }

        internal static List<Model.FamilyInfo> GetFamiliesByCategoryIdnew(Model.ConnectionConext connCtx, Guid cateId, string str, string searchString)
        {
            return FamilyDAL.GetFamiliesByCategoryIdnew(connCtx, cateId, str, searchString);
        }

        internal static List<Model.FamilyInfo> GetFamiliesByVendorId(Model.ConnectionConext connCtx, int vendorId, string searchString)
        {
            return FamilyDAL.GetFamiliesByVendorId(connCtx, vendorId,searchString);
        }

        internal static List<Model.FamilyInfo> GetFamiliesByStandardId(Model.ConnectionConext connCtx, int standardId)
        {
            return FamilyDAL.GetFamiliesByStandardId(connCtx, standardId);
        }

        internal static List<Model.FamilyInfo> GetFamiliesByCategoryVendorId(Model.ConnectionConext connCtx, Guid cateId, int vendorId, string searchString,string str)
        {
            return FamilyDAL.GetFamiliesByCategoryVendorId(connCtx, cateId, vendorId, searchString,str);
        }

        internal static List<Model.FamilyInfo> SearchFamilies(Model.ConnectionConext connCtx, string searchString)
        {
            return FamilyDAL.SearchFamilies(connCtx, searchString);
        }

        internal static List<Model.FamilyInfo> SearchFamiliesByVendor(Model.ConnectionConext connCtx, string searchString,int vendorid)
        {
            return FamilyDAL.SearchFamiliesByVendor(connCtx, searchString,vendorid);
        }

        internal static System.Data.DataTable GetSubFamilyTable(Model.ConnectionConext connCtx, Guid familyId, List<Model.ParameterValueInfo> paramConstraints)
        {
            return FamilyTableDAL.GetSubFamilyTable(connCtx, familyId, paramConstraints);
        }

        internal static string GetTemplateFile(Model.ConnectionConext connCtx, Guid familyId)
        {
            string familyCacheFodler = String.Format(@"c:\SASystem\{0}\", familyId);
            if (!System.IO.Directory.Exists(familyCacheFodler))
	        {
		        System.IO.Directory.CreateDirectory(familyCacheFodler);
	        }
            
            Model.FamilyAttrInfo familyAttrInfo = FamilyAttrDAL.GetFamilyAttrInfo(connCtx, familyId, Model.FamilyAttrType.kTemplateFile);
            string iptFileName = familyAttrInfo.TemplateFileName;

            string strTemplateFile = System.IO.Path.Combine(familyCacheFodler, iptFileName);

            if (!System.IO.File.Exists(strTemplateFile))
            {
                //save byte[] to file.
                //
                SaveToPath(familyAttrInfo.TemplateFileBinary, strTemplateFile);
            }
            
            return strTemplateFile;

            //get relative category path.
            //
            /*
            Model.FamilyInfo familyInfo = FamilyDAL.GetFamilyInfo(connCtx, familyId);
            Model.CategoryInfo categoryInfo = CategoryDAL.GetCateInfoById(connCtx, familyInfo.CategoryID);

            while (categoryInfo.Id != Guid.Empty)
            {
                categoryInfo = CategoryDAL.GetCateInfoById(connCtx, categoryInfo.ParentId);
            }
            */
        }

        internal static void SaveToPath(Byte[] streams, string filePath)
        {
            System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Create, FileAccess.Write);

            fs.Write(streams, 0, streams.Length);

            fs.Close();
        }

        internal static bool UpdateFamilyCachedFlag(Model.ConnectionConext connCtx, Guid familyId, bool isCached)
        {
            return FamilyDAL.UpdateFamilyCachedFlag(connCtx, familyId, isCached);
        }
    }
}
